package com.gyul.findmystock.db;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.gyul.findmystock.analysis.Gene;
import com.gyul.findmystock.entity.AnalysisEntity;
import com.gyul.findmystock.util.Constants;


public class DaoAnalysis extends DataAccessObject<Gene> {
	public static final String ANALYSIS_TABLE = "AnalysisTable";
	public static final String ANALYSIS_SYMBOL = "AnalysisSymbol";
	public static final String ANALYSIS_SHORT = "AnalysisShort";
	public static final String ANALYSIS_LONG = "AnalysisLong";
	public static final String ANALYSIS_MACD = "AnalysisMacd";
	public static final String ANALYSIS_PROFIT = "AnalysisProfit";
	public static final String ANALYSIS_STATUS = "AnalysisStatus";
	public static final String ANALYSIS_TRADE_INDEX = "AnalysisTradeIndex";
	public static final String ANALYSIS_TRADE_CLOSE = "AnalysisTradeClose";
	public static final String ANALYSIS_START = "AnalysisStartIndex";
	public static final String ANALYSIS_UPDATE = "AnalysisUpdateTime";
	public static final String ANALYSIS_VALIDITY = "AnalysisValidity";
		
	public static final String CREATE_QUERY = "CREATE TABLE IF NOT EXISTS " + ANALYSIS_TABLE + " ( " +
			ANALYSIS_SYMBOL + " TEXT PRIMARY KEY, " +
			ANALYSIS_SHORT + " INTEGER NOT NULL, " +
			ANALYSIS_LONG + " INTEGER NOT NULL, " +
			ANALYSIS_MACD + " INTEGER NOT NULL, " +
			ANALYSIS_PROFIT + " INTEGER NOT NULL, " +
			ANALYSIS_STATUS + " INTEGER NOT NULL, " +
			ANALYSIS_TRADE_INDEX + " INTEGER NOT NULL, " +
			ANALYSIS_TRADE_CLOSE + " INTEGER NOT NULL, " +
			ANALYSIS_START + " INTEGER NOT NULL, " +
			ANALYSIS_UPDATE + " INTEGER NOT NULL, " +
			ANALYSIS_VALIDITY + " INTEGER NOT NULL );";
	
	public static final String DROP_QUERY = "DROP TABLE IF EXISTS " + ANALYSIS_TABLE;
	
	public DaoAnalysis(String TAG) {		
		super(TAG + " - DaoAnalysis");
	}

	public void createTable(SQLiteDatabase db) {
		db.execSQL(CREATE_QUERY);
	}
	
	public void dropTable() {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(DROP_QUERY);
		//db.close();
	}
	
	public void insert(AnalysisEntity data) {
		SQLiteDatabase db = helper.getWritableDatabase();
		
		db.execSQL("INSERT INTO " + ANALYSIS_TABLE + 
				" (" + ANALYSIS_SYMBOL + ", " + ANALYSIS_SHORT + ", " + ANALYSIS_LONG + ", " +
					ANALYSIS_MACD + ", " + ANALYSIS_PROFIT + ", " + ANALYSIS_STATUS + ", " +
					ANALYSIS_TRADE_INDEX + ", " + ANALYSIS_TRADE_CLOSE + ", " +
					ANALYSIS_START + ", " + ANALYSIS_UPDATE +  ", " + ANALYSIS_VALIDITY + ") " +
					"VALUES ('" + data.symbol + "', " + data.shortMA + ", " +
								data.longMA + ", " + data.macdMA + ", " +
								data.profit + ", " + data.status + ", " +
								data.tradeIndex + ", " + data.tradeClose + ", " +
								data.startIndex + ", " + data.updateTime + ", " + data.validity + ");");
				
		//db.close();
	}
	
	public void update(AnalysisEntity data) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("INSERT OR REPLACE INTO " + ANALYSIS_TABLE + 
				" (" + ANALYSIS_SYMBOL + ", " + ANALYSIS_SHORT + ", " + ANALYSIS_LONG + ", " +
				ANALYSIS_MACD + ", " + ANALYSIS_PROFIT + ", " + ANALYSIS_STATUS + ", " +
				ANALYSIS_TRADE_INDEX + ", " + ANALYSIS_TRADE_CLOSE + ", " +
				ANALYSIS_START + ", " + ANALYSIS_UPDATE +  ", " + ANALYSIS_VALIDITY + ") " +
				"VALUES ('" + data.symbol + "', " + data.shortMA + ", " +
							data.longMA + ", " + data.macdMA + ", " +
							data.profit + ", " + data.status + ", " +
							data.tradeIndex + ", " + data.tradeClose + ", " +
							data.startIndex + ", " + data.updateTime + ", " + data.validity + ");");
		//db.close();
	}
	
	public ArrayList<AnalysisEntity> select() {
		SQLiteDatabase db = helper.getReadableDatabase();
		ArrayList<AnalysisEntity> list = new ArrayList<AnalysisEntity> ();
		Cursor c = db.rawQuery("SELECT * FROM " + ANALYSIS_TABLE + ";", null);
		while ( c.moveToNext() ) {
			list.add(new AnalysisEntity(c.getString(c.getColumnIndex(ANALYSIS_SYMBOL)),
					c.getInt(c.getColumnIndex(ANALYSIS_SHORT)),
					c.getInt(c.getColumnIndex(ANALYSIS_LONG)),
					c.getInt(c.getColumnIndex(ANALYSIS_MACD)),
					c.getInt(c.getColumnIndex(ANALYSIS_PROFIT)),
					c.getInt(c.getColumnIndex(ANALYSIS_STATUS)),
					c.getInt(c.getColumnIndex(ANALYSIS_TRADE_INDEX)),
					c.getInt(c.getColumnIndex(ANALYSIS_TRADE_CLOSE)),
					c.getInt(c.getColumnIndex(ANALYSIS_START)),
					c.getLong(c.getColumnIndex(ANALYSIS_UPDATE)),
					c.getInt(c.getColumnIndex(ANALYSIS_VALIDITY))));
		}
		
		c.close();
		//db.close();
		
		return list; 
	}
	public AnalysisEntity get(String symbol) {
		SQLiteDatabase db = helper.getReadableDatabase();
		AnalysisEntity analysisEntity = null;
		
		Cursor c = db.rawQuery("SELECT * FROM " + ANALYSIS_TABLE + " WHERE " + ANALYSIS_SYMBOL + " = '" + symbol + "';", null);
		
		if ( c.moveToNext() ) {
			analysisEntity = new AnalysisEntity(c.getString(c.getColumnIndex(ANALYSIS_SYMBOL)),
					c.getInt(c.getColumnIndex(ANALYSIS_SHORT)),
					c.getInt(c.getColumnIndex(ANALYSIS_LONG)),
					c.getInt(c.getColumnIndex(ANALYSIS_MACD)),
					c.getInt(c.getColumnIndex(ANALYSIS_PROFIT)),
					c.getInt(c.getColumnIndex(ANALYSIS_STATUS)),
					c.getInt(c.getColumnIndex(ANALYSIS_TRADE_INDEX)),
					c.getInt(c.getColumnIndex(ANALYSIS_TRADE_CLOSE)),
					c.getInt(c.getColumnIndex(ANALYSIS_START)),
					c.getLong(c.getColumnIndex(ANALYSIS_UPDATE)),
					c.getInt(c.getColumnIndex(ANALYSIS_VALIDITY)));
		}
		
		c.close();
		//db.close();
		
		return analysisEntity; 
	}
	
	public AnalysisEntity getNeedUpdateCompany() {
		SQLiteDatabase db = helper.getReadableDatabase();
		AnalysisEntity analysisEntity = null;
		
		Cursor c = db.rawQuery("SELECT * FROM " + ANALYSIS_TABLE + 
				" WHERE " + ANALYSIS_VALIDITY + " >= " + Constants.VALIDITY_ANALYSIS + " ORDER BY " + ANALYSIS_UPDATE + " ASC LIMIT 1;", null);
		
		if ( c.moveToNext() ) {
			analysisEntity = new AnalysisEntity(c.getString(c.getColumnIndex(ANALYSIS_SYMBOL)),
					c.getInt(c.getColumnIndex(ANALYSIS_SHORT)),
					c.getInt(c.getColumnIndex(ANALYSIS_LONG)),
					c.getInt(c.getColumnIndex(ANALYSIS_MACD)),
					c.getInt(c.getColumnIndex(ANALYSIS_PROFIT)),
					c.getInt(c.getColumnIndex(ANALYSIS_STATUS)),
					c.getInt(c.getColumnIndex(ANALYSIS_TRADE_INDEX)),
					c.getInt(c.getColumnIndex(ANALYSIS_TRADE_CLOSE)),
					c.getInt(c.getColumnIndex(ANALYSIS_START)),
					c.getLong(c.getColumnIndex(ANALYSIS_UPDATE)),
					c.getInt(c.getColumnIndex(ANALYSIS_VALIDITY)));
		}
		
		c.close();
		//db.close();
		
		return analysisEntity; 
	}
	
	public void delete(String symbol) {
		SQLiteDatabase db = helper.getWritableDatabase();
		
		db.execSQL("DELETE FROM " + ANALYSIS_TABLE + " WHERE " + ANALYSIS_SYMBOL + " = '" + symbol + "';");
		
		//db.close();
	}
}
